package io.github.fb513.transport.model;

import io.github.fb513.common.model.MessageInfo;
import io.github.fb513.transport.util.JsonUtils;
import io.github.fb513.transport.util.SessionHolder;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.Data;

/**
 * @author fengbo
 * @since 2023/12/25 13:55
 */
@Data
public class UserSession {

    private long heartBeatTime;

    private UserInfo userInfo;

    private NioSocketChannel channel;


    public void sendMsg(String message) {
        channel.writeAndFlush(message);
    }

    public void sendMsg(MessageInfo<?> message) {
        channel.writeAndFlush(JsonUtils.formatObjToJson(message));
    }

    public void close() {
        SessionHolder.remove(userInfo);
        channel.close();
    }
}
